package com.kwshare.common.core.domain;

import com.kwshare.common.core.constant.Constants;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author yangjing
 */
@Data
public class Response<T> implements Serializable {

    /** 成功标志 */
    public static final Integer SUCCESS = Constants.SUCCESS;

    /** 失败标志 */
    public static final Integer FAIL = Constants.FAIL;

    /** 状态码 */
    private int code;

    /** 信息 */
    private String message;

    /** 数据 */
    private T data;

    public static <T> Response<T> ok(){

        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> Response<T> ok(T data){

        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> Response<T> fail()
    {
        return restResult(null, FAIL, null);
    }

    public static <T> Response<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> Response<T> restResult(T data, int code, String message)
    {
        Response<T> apiResult = new Response<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(message);
        return apiResult;
    }
}
